package com.chalmers.foa.gamemodel.events.impl;

import java.util.List;

import com.chalmers.foa.gamemodel.events.IEvent;
import com.chalmers.foa.gamemodel.gameobjects.IGameObject;

public class EventChain implements IEvent {

	List<IGameObject> objects;
	int boundID, currentObject, lastObject;
	boolean reset;

	public EventChain(List<IGameObject> _objects, int _boundID,
			boolean _startState) {
		objects = _objects;
		boundID = _boundID;
		reset();
		switchActive();
	}

	@Override
	public void switchActive() {
		new Thread() {
			@Override
			public void run() {
				while (true) {
					try {
						reset = false;
						objects.get(currentObject).setAlive(true);
						Thread.sleep(1500);
						lastObject = currentObject;
						if (!reset) {
							currentObject++;
							if (currentObject == objects.size())
								currentObject = 0;
							objects.get(currentObject).setAlive(true);
							Thread.sleep(1500);
							objects.get(lastObject).setAlive(false);
						}

					} catch (InterruptedException e) {
					}
				}
			}

		}.start();
	}

	@Override
	public boolean isActive() {
		return true;
	}

	@Override
	public void reset() {
		currentObject = 0;
		for (IGameObject object : objects)
			object.setAlive(false);
		reset = true;
	}

	@Override
	public int getBoundID() {
		return boundID;
	}

}
